//
//  YCExpressViewController.m
//  lianganyuchu
//
//  Created by iMac on 17/3/3.
//  Copyright © 2017年 杭州桑缇电子商务有限公司. All rights reserved.
//

#import "YCExpressViewController.h"
#define YCOrderStatusCellIdentify @"YCOrderStatusTableViewCell"
#define YCExpress1CellIdentify @"YCExpress1TableViewCell"
#define YCExpress2CellIdentify @"YCExpress2TableViewCell"

@interface YCExpressViewController ()
{
    UITableView *m_tableView;
    UIView *m_footerView;
    UIButton *m_btnCancel;

    OrderExpressResponse *_response;
}
@end

@implementation YCExpressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"物流配送";
    self.navigationItem.leftBarButtonItem=[self tbarBackButton];
    
    [self addMainView];
    [self loadData];
}
-(void)loadData
{
    OrderExpressRequest *request = [OrderExpressRequest new];
    [apiClient hideProgress];
    [apiClient disableAfterRequest];
    request.order_no=_order.order_no;
    [apiClient doOrderExpress:request success:^(ApiResponse *data, NSString *url) {
        _response=[[OrderExpressResponse alloc]initWithCGResponse:data];
        [m_tableView reloadData];
        
        m_btnCancel.hidden=(_response.re_order.intValue==0);
        m_footerView.hidden=(_response.re_order.intValue==0);
        CGFloat bottom=-60;
        if (_response.re_order.intValue==0) {
            bottom=0;
        }
        [m_tableView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view).offset(0);
            make.right.equalTo(self.view).offset(0);
            make.top.equalTo(self.view).offset(0);
            make.bottom.equalTo(self.view).offset(bottom);
        }];
        
    }failure:^(ApiResponse *data, NSString *url){
    }];
}
-(void)addMainView
{
    m_tableView=[[UITableView alloc]init];
    m_tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    m_tableView.delegate=self;
    m_tableView.dataSource=self;
    m_tableView.showsVerticalScrollIndicator=NO;
    [self.view addSubview:m_tableView];
    [m_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        make.top.equalTo(self.view).offset(0);
        make.bottom.equalTo(self.view).offset(-60);
    }];
    [m_tableView registerNib:[UINib nibWithNibName:YCOrderStatusCellIdentify bundle:nil] forCellReuseIdentifier:YCOrderStatusCellIdentify];
    [m_tableView registerNib:[UINib nibWithNibName:YCExpress1CellIdentify bundle:nil] forCellReuseIdentifier:YCExpress1CellIdentify];
    [m_tableView registerNib:[UINib nibWithNibName:YCExpress2CellIdentify bundle:nil] forCellReuseIdentifier:YCExpress2CellIdentify];
    
    m_footerView=[[UIView alloc]init];
    [m_footerView setBackgroundColor:WHITE_COLOR];
    [self.view addSubview:m_footerView];
    [m_footerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        make.bottom.equalTo(self.view).offset(0);
        make.height.mas_equalTo(60);
    }];
    [m_footerView addShadowWith:CGSizeMake(kMainScreen_Width, 60)];
   
    m_btnCancel=[[UIButton alloc]init];
    [m_btnCancel setTitleColor:MAIN_COLOR forState:UIControlStateNormal];
    [m_btnCancel setTitle:@"取消配送" forState:UIControlStateNormal];
    [m_btnCancel.titleLabel setFont:[UIFont LightFontOfSize:16]];
    m_btnCancel.layer.borderColor=MAIN_COLOR.CGColor;
    m_btnCancel.layer.borderWidth=1.0f;
    m_btnCancel.layer.cornerRadius=2.0f;
    [m_btnCancel addTarget:self action:@selector(btnCancelClicked:) forControlEvents:UIControlEventTouchUpInside];
    [m_footerView addSubview:m_btnCancel];
    [m_btnCancel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(-kDistance);
        make.centerY.equalTo(m_footerView.centerY);
        make.width.mas_equalTo(120);
        make.height.mas_equalTo(40);
    }];
}
-(void)btnCancelClicked:(UIButton *)button
{
    OrderHandelRequest *request = [OrderHandelRequest new];
    [apiClient hideProgress];
    request.order_no=_order.order_no;
    request.type=@"6";
    [apiClient doOrderHandel:request success:^(ApiResponse *data, NSString *url) {
        [self.view showHUD:data.message];
        self.cancelBlock();
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self goBack];
        });
    }failure:^(ApiResponse *data, NSString *url){
    }];

}
#pragma mark tableview datasource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
#pragma mark tableview delegate
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        return 55;
    }
    if (indexPath.section==1) {
        return 155;
    }
    return 200;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"cell%ld%ld",(long)indexPath.section,(long)indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (nil == cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    if (indexPath.section==0) {
        YCOrderStatusTableViewCell *_cell = (YCOrderStatusTableViewCell *)[tableView dequeueReusableCellWithIdentifier:YCOrderStatusCellIdentify forIndexPath:indexPath];
        [_cell.backView setBackgroundColor:ORANGE_COLOR];
        [_cell.labelStatus setText:_response.status];
        cell=_cell;
    }
    if (indexPath.section==1) {
        YCExpress1TableViewCell *_cell = (YCExpress1TableViewCell *)[tableView dequeueReusableCellWithIdentifier:YCExpress1CellIdentify forIndexPath:indexPath];
         [_cell.labNum setText:_order.order_no];
         [_cell.labTime setText:_order.meal_time];
        cell=_cell;
    }
    if (indexPath.section==2) {
        YCExpress2TableViewCell *_cell = (YCExpress2TableViewCell *)[tableView dequeueReusableCellWithIdentifier:YCExpress2CellIdentify forIndexPath:indexPath];
        [_cell.labelTime setText:_response.createTime];
        [_cell.labelName setText:_response.transporterName];
        [_cell.labelPhone setText:_response.transporterPhone];
        cell=_cell;
    }
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


@end
